package occ.service
{
  import flash.data.*;
  import flash.events.*;
  import flash.events.EventDispatcher;
  import flash.events.IEventDispatcher;
  
  import mx.collections.ArrayCollection;
  
  import occ.utils.*;

  public class Common extends EventDispatcher
  {
    public static const GET_STATES:String="GetStatesEvent";
    
    public static const arStates:Array = new Array("AL","AK","AR", "AZ", "CA", "CO", "CT", "DE", "DC", "FL",
                                      "GA","HI","ID", "IL","IN","IA","KS","KY","LA","ME","MD","MA","MI",
                                      "MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH",
                                      "OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV",
                                      "WI","WY");
                                      
    public static const states:ArrayCollection =new ArrayCollection(arStates);
    
    private var sqlData:ArrayCollection;
		private var stmt:SQLStatement;
		private var sql:String;
		
    public var states:ArrayCollection;
    public function Common(target:IEventDispatcher=null)
    {
      super(target);
    }
    
    public function getStates():void
    {
      sql="select state_short as state from states";
      stmt = new SQLStatement();
			stmt.sqlConnection = DatabaseManager.getConnection();
			
			stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			stmt.addEventListener( SQLEvent.RESULT, onGetStatesResult );
			stmt.text = sql;
			stmt.execute();
    }
    
    private function onGetStatesResult(event:SQLEvent):void
    {
      var result:SQLResult = stmt.getResult();
			 states= new ArrayCollection();
			 sqlData = new ArrayCollection( result.data );
			 for(var i:int=0;i<sqlData.length;i++){
			   states.addItem((sqlData[i] as Object).state);
			 }
			 //trace(" the cnt: " + (sqlData[0] as Object).name);
			 dispatchEvent(new Event(GET_STATES));
    }
    
    private function onSQLError( event:SQLErrorEvent ):void
		{
			 var errorString:String = "SQL Error: " + event.error.errorID + "nDetails: " + event.error.details + "nDetail Arguments: " +event.error.detailArguments;
 			throw new Error(errorString);
		}
    
  }
}